package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"order_srv/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func Init() {
	var Conf = appconfig.Con.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Conf.User, Conf.Password, Conf.Host, Conf.Port, Conf.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("mysql连接失败")
	}
	fmt.Println(Conf)
	fmt.Println("mysql连接成功")
	err = DB.Debug().AutoMigrate(&Goods{}, &OrderGoods{}, &Order{}, &User{})
	if err != nil {
		panic("迁移失败")
	}
	fmt.Println("迁移成功")
}
